<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion">后台管理</i>
      </el-breadcrumb-item>
      <el-breadcrumb-item>添加相册</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>

    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="简介" prop="description">
        <el-input v-model="ruleForm.description"></el-input>
      </el-form-item>
      <el-form-item label="排序序号" prop="sort">
        <el-input v-model="ruleForm.sort"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')"> 添加 </el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具 js，第三方插件 js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';

export default {
  //import 引入的组件需要注入到对象中才能使用

  name: "AlbumAddNew",
  components: {},
  props: {},
  data() {
    //这里存放数据
    return {
      ruleForm: {
        name: "",
        description: "",
        sort: "",
      },
      rules: {
        name: [
          { required: true, message: "请输入相册名称", trigger: "blur" },
          {
            min: 4,
            max: 15,
            message: "长度在 4 到 15 个字符",
            trigger: "blur",
          },
        ],
        description: [
          { required: true, message: "请输入相册简介", trigger: "blur" },
          {
            min: 2,
            max: 35,
            message: "长度在 2 到 35 个字符",
            trigger: "blur",
          },
        ],
        sort: [
          { required: true, message: "请输入相册排序序号", trigger: "blur" },
          {
            pattern: /^(\d{1}|[1-9]{1}[0-9]?)$/,
            message: "相册排序序号必须是 0 ~ 99 之间的数字",
            trigger: "blur",
          },
        ],
      },
    };
  },
  //计算属性 类似于 data 概念
  computed: {},
  //监控 data 中的数据变化
  watch: {},
  //方法集合
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // alert("submit!");
          let url = "http://localhost:8088/albums/add-new";
          console.log("url = " + url);
          let formData = this.qs.stringify(this.ruleForm);
          console.log("formData = " + formData);
          this.axios
            .create({
              headers: {
                Authorization: localStorage.getItem("jwt"),
              },
            })
            .post(url, formData)
            .then((response) => {
              let responseBody = response.data;
              if (responseBody.state == 20000) {
                this.$message({
                  showClose: true,
                  message: "添加相册成功！",
                  type: "success",
                });
                this.resetForm(formName);
              } else {
                this.$message.error(responseBody.message);
              }
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
  //生命周期 - 创建完成（可以访问当前 this 实例）
  created() {},
  //生命周期 - 挂载完成（可以访问 DOM 元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有 keep-alive 缓存功能，这个函数会触发
};
</script>
<style scoped></style>
